package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMajor;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysMajorService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 专业管理Controller
 *
 * @author ruoyi
 * @date 2020-12-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/major")
public class SysMajorController extends BaseController {

    private final ISysMajorService iSysMajorService;

    /**
     * 查询专业管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMajor sysMajor) {
        startPage();
        List<SysMajor> list = iSysMajorService.queryList(sysMajor);
        return getDataTable(list);
    }

    /**
     * 返回所有启用专业
     */
    @GetMapping("/listAllEnabled")
    public AjaxResult listAllEnabled() {
        List<SysMajor> list = iSysMajorService.queryList(new SysMajor().setStatus("0"));
        return AjaxResult.success(list);
    }

    /**
     * 返回用户的启用专业
     */
    @GetMapping("/listUserEnabled")
    public AjaxResult listUserEnabled(@RequestParam(defaultValue = "false") boolean listDisabled) {
        List<SysMajor> list = iSysMajorService.listUserEnabled(listDisabled);
        return AjaxResult.success(list);
    }

    /**
     * 导出专业管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:major:export')")
    @Log(title = "专业管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysMajor sysMajor) {
        List<SysMajor> list = iSysMajorService.queryList(sysMajor);
        ExcelUtil<SysMajor> util = new ExcelUtil<SysMajor>(SysMajor.class);
        return util.exportExcel(list, "major");
    }

    /**
     * 获取专业管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:major:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iSysMajorService.getById(id));
    }

    /**
     * 新增专业管理
     */
    @PreAuthorize("@ss.hasPermi('system:major:add')")
    @Log(title = "专业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMajor sysMajor) {
        return toAjax(iSysMajorService.save(sysMajor) ? 1 : 0);
    }

    /**
     * 修改专业管理
     */
    @PreAuthorize("@ss.hasPermi('system:major:edit')")
    @Log(title = "专业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMajor sysMajor) {
        return toAjax(iSysMajorService.updateById(sysMajor) ? 1 : 0);
    }

    /**
     * 删除专业管理
     */
    @PreAuthorize("@ss.hasPermi('system:major:remove')")
    @Log(title = "专业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSysMajorService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
